/**
 * Filename: NewsInviteAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/4 16:18
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNNewsInvite;
import com.github.postsanf.yinian.fragment.NewsInviteFragment;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.TimeUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.util.LinkedList;
/**
 * 邀请适配器
 */
public class NewsInviteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context context;
    private LinkedList<YNNewsInvite> datas;
    private NewsInviteFragment  fragment;
    private static final int EMPTY_VIEW = 10;

    public NewsInviteAdapter(NewsInviteFragment fragment,Context context, LinkedList<YNNewsInvite> datas) {
        this.context = context;
        this.datas   = datas;
        this.fragment = fragment;
    }

    public class EmptyViewHolder extends RecyclerView.ViewHolder {
        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView    iv_news_invite_avatar;						// 邀请人头像
        TextView 	        tv_news_invite_name;				        // 邀请人名
        TextView 	        tv_news_invite_time;				        // 邀请时间
        TextView 	        tv_news_invite_content;				        // 邀请内容
        Button              news_invate_yes;				            // 接受邀请按钮
        Button              news_invate_no;				                // 忽略邀请按钮

        public ViewHolder(View itemView) {
            super(itemView);
            iv_news_invite_avatar = (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_news_invite_avatar);
            tv_news_invite_name = (TextView) itemView
                    .findViewById(R.id.tv_news_invite_name);
            tv_news_invite_time = (TextView) itemView
                    .findViewById(R.id.tv_news_invite_time);
            tv_news_invite_content = (TextView) itemView
                    .findViewById(R.id.tv_news_invite_content);
            news_invate_yes = (Button) itemView
                    .findViewById(R.id.news_invate_yes);
            news_invate_no = (Button) itemView
                    .findViewById(R.id.news_invate_no);
        }

        public void bindData(final YNNewsInvite listItems, final int position)
        {
            if (listItems != null)
            {
                // 评论人头像
                String avatarPaths = YNUrlHelper.getAvatarUrl(context,listItems.getUpic());
                Uri avatarUri = Uri.parse(avatarPaths);
                ImageOptHelper.showFrescoAvatar(avatarUri, iv_news_invite_avatar, context);

                tv_news_invite_name.setText(listItems.getUnickname());
                tv_news_invite_content.setText(listItems.getIgcontent());
                tv_news_invite_time.setText(TimeUtils.getMsgTime(listItems.getIgtime()));

                news_invate_yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        fragment.invateYes(listItems.getType(), position, String.valueOf(listItems.getIggroupid()), String.valueOf(listItems.getIgid()));
                    }
                });

                news_invate_no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        fragment.deleteNews(position, String.valueOf(listItems.getIgid()), true);
                    }
                });
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == EMPTY_VIEW) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_empty_news, parent, false);
            v.getRootView().getLayoutParams().height = DisplayUtils.getScreenHeightPixels((Activity) context)-DisplayUtils.dp2px(context,160);
            return new EmptyViewHolder(v);
        }

        final View convertView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.yn_item_news_invite, parent, false);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rv_holder, int position){
        if (rv_holder instanceof ViewHolder){
            final ViewHolder holder = (ViewHolder) rv_holder;
            YNNewsInvite listItems = getItem(position);
            holder.bindData(listItems, position);
            holder.itemView.setTag(listItems);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0) {
            return EMPTY_VIEW;
        }
        return super.getItemViewType(position);
    }

    private YNNewsInvite getItem(int position) {
        return datas.get(position);
    }

    @Override
    public int getItemCount() {
        return datas.size() > 0 ? datas.size() : 1 ;
    }
}
